import { App } from "ionic-angular";
import { Config } from "ionic-angular";
import { isPresent } from "ionic-angular/util/util";
import { PORTAL_LOADING } from "ionic-angular/components/app/app-constants";
import { ReloadingCmp } from "./reloading-component";
import { LoadingOptions } from "ionic-angular";
import { LoadingMdPopIn, LoadingMdPopOut, LoadingPopIn, LoadingPopOut, LoadingWpPopIn,
  LoadingWpPopOut } from "ionic-angular/components/loading/loading-transitions";
import { NavOptions } from "ionic-angular";
import { ViewController } from "ionic-angular";

/**
 * @hidden
 */
export class Reloading extends ViewController {
  private _app: App;

  constructor(app: App, opts: LoadingOptions = {}, config: Config) {
    opts.showBackdrop = isPresent(opts.showBackdrop) ? !!opts.showBackdrop : true;
    opts.enableBackdropDismiss = isPresent(opts.enableBackdropDismiss) ? !!opts.enableBackdropDismiss : false;
    opts.dismissOnPageChange = isPresent(opts.dismissOnPageChange) ? !!opts.dismissOnPageChange : false;
    opts.cssClass = isPresent(opts.cssClass) ? opts.cssClass + ' reloading' : 'reloading';

    super(ReloadingCmp, opts, null);
    this._app = app;
    this.isOverlay = true;

    config.setTransition('loading-pop-in', LoadingPopIn);
    config.setTransition('loading-pop-out', LoadingPopOut);
    config.setTransition('loading-md-pop-in', LoadingMdPopIn);
    config.setTransition('loading-md-pop-out', LoadingMdPopOut);
    config.setTransition('loading-wp-pop-in', LoadingWpPopIn);
    config.setTransition('loading-wp-pop-out', LoadingWpPopOut);
  }

  /**
   * @hidden
   */
  getTransitionName(direction: string): string {
    let key = (direction === 'back' ? 'loadingLeave' : 'loadingEnter');
    return this._nav && this._nav.config.get(key);
  }

  /**
   * @param {string} content sets the html content for the loading indicator.
   */
  setContent(content: string): Reloading {
    this.data.content = content;
    return this;
  }

  /**
   * @param {string} spinner sets the name of the SVG spinner for the loading indicator.
   */
  setSpinner(spinner: string): Reloading {
    this.data.spinner = spinner;
    return this;
  }

  /**
   * @param {string} cssClass sets additional classes for custom styles, separated by spaces.
   */
  setCssClass(cssClass: string): Reloading {
    this.data.cssClass = cssClass;
    return this;
  }

  /**
   * @param {boolean} showBackdrop sets whether to show the backdrop.
   */
  setShowBackdrop(showBackdrop: boolean): Reloading {
    this.data.showBackdrop = showBackdrop;
    return this;
  }

  /**
   * @param {number} dur how many milliseconds to wait before hiding the indicator.
   */
  setDuration(dur: number): Reloading {
    this.data.duration = dur;
    return this;
  }

  /**
   * Present the loading instance.
   *
   * @param {NavOptions} [navOptions={}] Nav options to go with this transition.
   * @returns {Promise} Returns a promise which is resolved when the transition has completed.
   */
  present(navOptions: NavOptions = {}): Promise<any> {
    return this._app.present(this, navOptions, PORTAL_LOADING);
  }

  /**
   * Dismiss all loading components which have been presented.
   */
  dismissAll() {
    this._nav && this._nav.popAll();
  }

}

